require.config({
    baseUrl: baseUrl
});

define(function(require) {
    var $ = require('jquery');
    window.$ = window.jQuery = $;
    var dialog = require('bDialog/dialog');
    window.dialog=dialog;
    require('webui');
    var c = require('../common');
    require('MobileSlider');

    $(function() {
        c.init();

        //图片截取
        $('.pic110 img').imageZoom(110,110);

        //tab
        $('.tab-tit a').click(function(event) {
            var index=$(this).index();
            $(this).addClass('active').siblings('a').removeClass('active');
            $('.tab').find('.tab-con').eq(index).show().siblings('.tab-con').hide();
        });

        //选购========        
        //减
        var format_price=0;
        $('.numbox .reduce').click(function(){
            var t=$(this);
            var ipt=t.siblings('.tb');
            var v=ipt.val();
            var n=v;
            v=parseInt(v);
            v=(v-1)<0?0:(v-1);
            ipt.val(v);
            
            var type=t.parents('li').data('format');
            if(type==2){//有规格分类计算价格
                if(v==0){t.parent().siblings('.saleprice').removeClass('hide');}
                if(n==0){
                    var unit_price=0;
                }else{
                    var unit_price=parseFloat(t.parents('li').children('.item').last().find('.unit_fprice').html());
                }
                format_price-=unit_price;
                var tag = t.parents('li').children('.item').last().find('.format a').data('tag');
                t.parents('li').children('.item').last().remove();
            }
            cur_num(t);

            var product_id = t.data('id');
            var cid = t.data('cid');
            del(product_id,cid,tag);
        });

        //删除购物车商品
        function del(product_id,cid,tag){
            var shop_id = $('input[name="shop_id"]').val();
            if(empty(product_id) || empty(cid) || empty(shop_id)){
                c.error('参数错误!',2000);
                return false;
            }
            var forms = {};
            forms['id'] = product_id;
            forms['cid'] = cid;
            forms['sid'] = shop_id;
            forms['tag'] = tag;
            $.post(URL+"/del",forms,function(data){
                cart(data.data['num'],data.data['price']);
            });
        }

        //加
        $('.numbox .add').click(function(){
            var t=$(this);
            var ipt=t.siblings('.tb');
            var v=ipt.val();
            v=parseInt(v);
            v=v+1;
            ipt.val(v);

            var type=t.parents('li').data('format');
            if(type==2){//有规格分类计算价格
                t.parent().siblings('.saleprice').addClass('hide');
                var id = t.data('id');
                var sid = $('input[name="shop_id"]').val();
                //获取属性
                $.get(URL+"/getAttr?id="+id+"&num="+v+"&sid="+sid,function(data){
                    if(data.status == 1){
                        t.parents('li').append(data.data);
                        var attr_key = t.parents('li').find('.item').eq(v-1).find('.format a.active').data('key');
                        var product_id = t.data('id');
                        var cid = t.data('cid');
                        add(product_id,cid,attr_key);
                    }
                });
            }else{
                var product_id = t.data('id');
                var cid = t.data('cid');
                add(product_id,cid,'');
            }
            cur_num(t);
        });

        //增加请求方法
        function add(product_id,cid,attr_key){
            var shop_id = $('input[name="shop_id"]').val();
            if(empty(product_id) || empty(cid) || empty(shop_id)){
                c.error('参数错误!',2000);
                return false;
            }
            var forms = {};
            forms['id'] = product_id;
            forms['cid'] = cid;
            forms['attr'] = attr_key;
            forms['sid'] = shop_id;
            $.post(URL+"/add",forms,function(data){
                cart(data.data['num'],data.data['price']);
            });
        }

        //属性选择事件
        $('.goodslist').delegate('.format a', 'click', function(event) {
            var t=$(this);
            var price=t.data('price');
            t.addClass('active').siblings('a').removeClass('active');
            t.parents('.item').find('.unit_fprice').html(price);
            format_price = formatcart();
            cart(format_price);
            var attr_key = t.data('key');
            var product_id = t.data('id');
            var cid = t.data('cid');
            var tag = t.data('tag');
            update(product_id,cid,attr_key,tag);
        });

        //更新购物车
        function update(product_id,cid,attr_key,tag){
            var shop_id = $('input[name="shop_id"]').val();
            if(empty(product_id) || empty(cid) || empty(shop_id)){
                c.error('参数错误!',2000);
                return false;
            }
            var forms = {};
            forms['id'] = product_id;
            forms['cid'] = cid;
            forms['attr'] = attr_key;
            forms['sid'] = shop_id;
            forms['tag'] = tag;
            $.post(URL+"/update",forms,function(data){
                cart(data.data['num'],data.data['price']);
            });
        }


        function formatcart(){
            var fp=0;
            $('.goodslist li .item').each(function(){
                var t=$(this);                
                var unit_price=parseFloat(t.find('.unit_fprice').html());
                fp+=unit_price;
            });
            return fp;
        }

        function cart(num,price){
            $('.total_num').show().html(num);
            $('.total_price').html(price);
        }
        //menu当前选中项数量
        function cur_num(t){
            var num=0;
            t.closest('.goodslist').find('li').each(function(){
                var t=$(this);
                var qty=parseInt(t.find('.tb').val());
                num+=qty;
            });
            if(num!=0){
                $('.menu').find('a.active em').show().html(num);
            }else{
                $('.menu').find('a.active em').hide().html(num);
            }            
        }

        /*var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        var outer = isAndroid ? 'wrapper' : 'container';
        var list = [outer];
        var prevent = new PreventOverScroll({
            list: list
        });*/

        //查看商品详情
        $('.prodetail').click(function(event) {
            var id = $(this).data('id');
            if(empty(id)){
                c.error('请选择操作项!',2000);
                return false;
            }
            var title = $('.header .f28').data('title');
            dialog({skin:'detail',width:520,padding:'0',title:title,url:URL+'/detail?id='+id,end:function(){
                $('.detail-banner').MobileSlider({width:400,height:400,auto:false});
            }});
        });

        //搜索跳转
        $('.icon-search').click(function(){
            var shop_id = $('input[name="shop_id"]').val();
            if(empty(shop_id)){
                c.error('请选择分店！',2000);
                return false;
            }
            location.href = URL+"/searchs?id="+shop_id;
        });

        //搜索
        $('.btn-search').click(function(){
            var shop_id = $('input[name="shop_id"]').val();
            var kw = $.trim($('input[name="kw"]').val());
            if(empty(shop_id)){
                c.error('请选择分店！',2000);
                return false;
            }
            if(kw){
                location.href = URL+"/searchs?id="+shop_id+"&keyword="+kw;
            }
        });

    });
});